package com.linked.basemanage.switchsetting.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linked.basemanage.base.bean.param.SwitchSettingPageParam;
import com.linked.basemanage.base.bean.po.SwitchInfoPO;
import com.linked.basemanage.config.RedisKeysConfig;
import com.linked.basemanage.switchsetting.mapper.ISwitchSettingMapper;
import com.linked.basemanage.switchsetting.service.ISwitchSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author :dbq
 * @date : 2022/11/24 8:33
 */
@Slf4j
@Service
public class SwitchSettingServiceImpl implements ISwitchSettingService {

    private final ISwitchSettingMapper switchSettingMapper;

    private final StringRedisTemplate redisTemplate;

    public SwitchSettingServiceImpl(ISwitchSettingMapper switchSettingMapper, StringRedisTemplate redisTemplate) {
        this.switchSettingMapper = switchSettingMapper;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public IPage<SwitchInfoPO> querySwtichSettingPage(SwitchSettingPageParam param) throws Exception {
        return switchSettingMapper.querySwtichSettingPage(param);
    }

    @Override
    public boolean saveSwitchInfo(SwitchInfoPO param) throws Exception {
        return switchSettingMapper.insert(param) > 0;
    }

    @Override
    public boolean updateSwitchInfo(SwitchInfoPO temp) throws Exception {
        return switchSettingMapper.updateById(temp) > 0;
    }

    @Override
    public SwitchInfoPO findSwitchByName(String switchName) throws Exception {
        return switchSettingMapper.findSwitchByName(switchName);
    }

    @Override
    public List<SwitchInfoPO> querySwitchList(SwitchInfoPO param) throws Exception {
        return switchSettingMapper.querySwitchList(param);
    }

    @Override
    public Boolean findSwitchStatus(String switchCode) {
        Boolean switchValue = null;
        //1、查询redis中存在开关信息吗
        if (redisTemplate.hasKey(RedisKeysConfig.KEY_SWITCHS) && redisTemplate.opsForHash().hasKey(RedisKeysConfig.KEY_SWITCHS, switchCode)) {
            return Boolean.TRUE.equals(redisTemplate.opsForHash().get(RedisKeysConfig.KEY_SWITCHS, switchCode));
        }
        //2、从数据库中读取
        SwitchInfoPO switchInfo = switchSettingMapper.findSwitchByCode(switchCode);
        if (!ObjectUtils.isEmpty(switchInfo)) {
            switchValue = switchInfo.getIfSwitch() == 1 ? true : false;
            redisTemplate.opsForHash().put(RedisKeysConfig.KEY_SWITCHS, switchCode, switchValue + "");
            return switchValue;
        }
        //3、从实体类中取默认值
        try {
            Class switchClass = Class.forName("com.linked.commonentity.basemanage.switchsetting.DefaultSwitchSetting");
            Field switchCodeField = switchClass.getDeclaredField(switchCode);
            if (!ObjectUtils.isEmpty(switchCodeField)) {
                switchValue = (boolean) switchCodeField.get(null);
                switchInfo = new SwitchInfoPO();
                switchInfo.setSwitchCode(switchCode);
                switchInfo.setIfSwitch(switchValue ? 1 : 0);
                switchInfo.setIfTime(0);
                switchInfo.setSwitchName("默认开关-" + switchCode);
                saveSwitchInfo(switchInfo);
                return switchValue;
            } else {
                log.warn("未配置开关：" + switchCode);
            }
        } catch (Exception e) {
            log.info("默认开关处理异常！", e);
        }
        return null;
    }

    @Override
    public Boolean turnSwitch(String switchCode) {
        if (redisTemplate.opsForHash().hasKey(RedisKeysConfig.KEY_SWITCHS, switchCode)) {
            redisTemplate.opsForHash().delete(RedisKeysConfig.KEY_SWITCHS, switchCode);
        }
        SwitchInfoPO switchInfo = switchSettingMapper.findSwitchByCode(switchCode);
        if (!ObjectUtils.isEmpty(switchInfo)) {
            switchInfo.setIfSwitch(switchInfo.getIfSwitch() == 1 ? 0 : 1);
            switchSettingMapper.updateById(switchInfo);
            return true;
        }
        return false;
    }
}
